<template>
	<view class="zai-box">
		<!-- <image src="../../static/zaizai-login/register.png" mode='aspectFit' class="zai-logo"></image>
		<view class="zai-title">全民防灾</view> -->
		<view class="zai-form">
			<input class="zai-input" placeholder-class placeholder="请输入用户名" v-model="name"/>
			<input class="zai-input" placeholder-class placeholder="请输入手机号码" v-model="phone" />
			<view class="zai-input-btn">
				<input class="zai-input" placeholder-class placeholder="验证码" v-model="mycode"/>
				<view class="zai-checking" @click="checking" v-if="state===false">获取验证码</view>
				<view class="zai-checking zai-time" v-if="state===true">倒计时{{ currentTime }}s</view>
			</view>
			<input class="zai-input" placeholder-class  placeholder="请输入邮箱" v-model="email"/>
			<input class="zai-input" placeholder-class password placeholder="请输入密码" v-model="password"/>
			<input class="zai-input" placeholder-class password placeholder="请再次输入密码" v-model="password2"/>
			<button class="zai-btn" @click="register()">立即注册</button>
			<navigator url="../login/login" open-type='navigateBack' hover-class="none" class="zai-label">已有账号，点此去登录.</navigator>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				state: false,		//是否开启倒计时
				totalTime: 60,		//总时间，单位秒
				recordingTime: 0, 	//记录时间变量
				currentTime: 0, 	//显示时间变量
				name:'',
				phone:'',
				code:'000000',
				mycode:'',
				password:'',
				password2:'',
				email:''
			}
		},
		onLoad() {
			
		},
		methods: {
			register(){
				if(this.name==""||this.password==""||this.phone==""){
					uni.showToast({
					    title: '请完善注册信息！',
					    duration: 2000,
						icon:'none'
					});	
				}
				else if(this.mycode != this.code){
					uni.showToast({
					    title: '验证码错误',
					    duration: 2000,
						icon:'none'
					});	
				}else if(this.password!=this.password2){
					uni.showToast({
					    title: '两次密码不一致',
					    duration: 2000,
						icon:'none'
					});	
				}else{
					uni.request({
					    url: 'http://47.93.42.155:8080/WePrevent/RegisterServlet', //
					    data: {
					       name:this.name,
						   phone:this.phone,
						   password:this.password,
							email:this.email
					    },
					    success: (res) => {
					        console.log(res.data);
							uni.showToast({
							    title: res.data,
							    duration: 2000,
							});	
							this.mycode=''
							this.name=''
							this.phone=''
							this.password=''
							this.password2=''
							
					    }
					});
				}
			},
			checking() {
				var reg =/^0?1[3|4|5|6|7|8][0-9]\d{8}$/;
				if(this.name == ""){
					uni.showToast({
					    title: '用户名未填写',
					    duration: 2000,
						icon:'none'
					});		
				}
				else if( !reg.test(this.phone)){
					uni.showToast({
					    title: '请输入正确手机号',
					    duration: 2000,
						icon:'none'
					});		
				}else{
					//把显示时间设为总时间
					this.currentTime = this.totalTime;
					//开始倒计时
					this.state = true;
					//执行倒计时
					this.checkingTime();
					this.toSendPhoneCode();
				}
				//
				
			},
			toSendPhoneCode(){
				
				uni.request({
					url:"http://47.93.42.155:8080/WePrevent/SendPhoneNumServlet?phone="+this.phone,
					success: (res) => {
						console.log(res.data.code);
						if(res.data.code=="0000"){
							uni.showToast({
							    title: '验证已发送',
							    duration: 2000,
								icon:'none'
							});	
							this.code=res.data.phoneCode;
						}else{
							uni.showToast({
							    title: '错误！请联系客服',
							    duration: 2000,
								icon:'none'
							});		
						}
					}
				})
			},
			checkingTime(){
				let that = this;
				//判断是否开启
				if(this.state){
					//判断显示时间是否已到0，判断记录时间是否已到总时间
					if(this.currentTime > 0 && this.recordingTime <= this.totalTime){
						//记录时间增加 1
						this.recordingTime = this.recordingTime + 1;
						//显示时间，用总时间 - 记录时间
						this.currentTime = this.totalTime - this.recordingTime;
						//1秒钟后，再次执行本方法
						setTimeout(() => { 	
							//定时器内，this指向外部，找不到vue的方法，所以，需要用that变量。
							that.checkingTime();
						}, 1000)
					}else{
						//时间已完成，还原相关变量
						this.state = false;		//关闭倒计时
						this.recordingTime = 0;	//记录时间为0
						this.currentTime = this.totalTime;	//显示时间为总时间
					}
				}else{
					//倒计时未开启，初始化默认变量
					this.state = false;
					this.recordingTime = 0;
					this.currentTime = this.totalTime;
				}
			}
		}
	}
</script>

<style>
	.zai-box{
		padding: 0 100upx;
		position: relative;
	}
	.zai-logo{
		width: 100%;
		width: 100%;
		height: 310upx;
	}
	.zai-title{
		position: absolute;
		top: 0;
		line-height: 360upx;
		font-size: 68upx;
		color: #fff;
		text-align: center;
		width: 100%;
		margin-left: -100upx;
	}
	.zai-form{
		margin-top: 100upx;
	}
	.zai-input{
		background: #e2f5fc;
		margin-top: 30upx;
		border-radius: 100upx;
		padding: 20upx 40upx;
		font-size: 36upx;
	}
	.input-placeholder, .zai-input{
		color: #94afce;
	}
	.zai-label{
		padding: 60upx 0;
		text-align: center;
		font-size: 30upx;
		color: #a7b6d0;
	}
	.zai-btn{
		background: #00cfa3;
		color: #fff;
		border: 0;
		border-radius: 100upx;
		font-size: 36upx;
		margin-top: 60upx;
	}
	.zai-btn:after{
		border: 0;
	}
	
	/*验证码输入框*/
	.zai-input-btn{
		position: relative;
	}
	.zai-input-btn .zai-input{
		padding-right: 260upx;
	}
	.zai-checking{
		position: absolute;
		right: 0;
		top: 0;
		background: #00cfa3;
		color: #fff;
		border: 0;
		border-radius: 110upx;
		font-size: 36upx;
		margin-left: auto;
		margin-right: auto;
		padding-left: 28upx;
		padding-right: 28upx;
		box-sizing: border-box;
		text-align: center;
		text-decoration: none;
		line-height: 2.55555556;
		-webkit-tap-highlight-color: transparent;
		overflow: hidden;
		padding-top: 2upx;
		padding-bottom: 2upx;
	}
	.zai-checking.zai-time{
		background: #a7b6d0;
	}
	
	/*按钮点击效果*/
	.zai-btn.button-hover{
		transform: translate(1upx, 1upx);
	}
</style>
